# 0.0 Initialize environment ----
source("src/initialize.R")

# 1.0 Tables ----
## 1.1 Table 1 ----
ds %>%
  drop_na(redist_correct) %>%
  filter(redist_type_two %in% c("Commission", "State_Leg")) %>%
  mutate(total_n = length(redist_correct)) %>%
  group_by(redist_type_two, redist_correct) %>%
  summarise(
    nrow = length(redist_correct),
    percent_of_total = unique(length(redist_correct) / total_n),
    total = unique(total_n)
  ) %>% 
  mutate(correct_per_type =  100 * nrow / sum(nrow))

## 1.2 Table 2 ----
summary(
  lm_robust(
    redist_satisfied_num_post ~ 
      redist_type_two + redist_correct + party_match18,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~ 
      redist_type_two + redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~ 
      redist_type_two * redist_correct + party_match18,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~ 
      redist_type_two * redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

# 2.0 Figures ----
## 2.1 Figure 1 ----
### 2.1.1 Plot ----
ds %>% 
  drop_na(redist_party_control_match) %>% 
  filter(redist_party_control_match != "Single_Seat") %>%
  count(redist_party_control_match) %>% 
  mutate(perc = percent(n / sum(n), accuracy = .1, trim = FALSE)) %>% 
  ggplot(., aes(x = redist_party_control_match, y = n)) +
  geom_col() +
  geom_text(aes(label = perc), color = "white", nudge_y = -15) +
  labs(title = "", x = "2022 Map Author", y = "Count") +
  scale_x_discrete(
    labels = c(
      "Copartisan" = "Leg: Copartisan", 
      "Opposition" = "Leg: Opposition", 
      "Independent" = "Leg: Independent",
      "Commission" = "Commission", 
      "Court" = "Court"
    )
  )

## 2.2 Figure 2 ----
### 2.2.1 Plot ----
ds %>% 
  drop_na(redist_satisfied_num_post) %>% 
  count(redist_satisfied_num_post) %>% 
  mutate(
    perc = percent(n / sum(n), accuracy = .1, trim = FALSE),
    redist_satisfied_num_post = as.factor(redist_satisfied_num_post)
  ) %>% 
  ggplot(., aes(x = redist_satisfied_num_post, y = n)) +
  geom_col() +
  geom_text(aes(label = perc), color = "white", nudge_y = -20) +
  labs(title = "State Redistricting Satisfaction", x = "Satisfaction Rating", y = "Count") +
  scale_x_discrete(
    labels = c(
      "1" = "1\n Very Dissatisfied", 
      "2" = "2\n ", 
      "3" = "3\n Neutral", 
      "4" = "4\n ", 
      "5" = "5\n Very Satisfied"
    )
  )

ds %>% 
  drop_na(redist_fair_num_post) %>% 
  count(redist_fair_num_post) %>% 
  mutate(
    perc = percent(n / sum(n), accuracy = .1, trim = FALSE),
    redist_fair_num_post = as.factor(redist_fair_num_post)
  ) %>% 
  ggplot(., aes(x = redist_fair_num_post, y = n)) +
  geom_col() +
  geom_text(aes(label = perc), color = "white", nudge_y = -20) +
  labs(title = "Trust in State Redistricting", x = "Trust Rating", y = "Count") +
  scale_x_discrete(
    labels = c(
      "1" = "1\n Not at all", 
      "2" = "2\n ", 
      "3" = "3\n ", 
      "4" = "4\n A great deal"
    )
  )

## 2.3 Figure 3 ----
### 2.3.1 Plot ----
ds %>%
  drop_na(redist_satisfied_num_post, redist_type_two) %>%
  group_by(redist_type_two) %>%
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post),
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  mutate(redist_type_two = fct_rev(redist_type_two)) %>%
  ggplot(., aes(x = redist_type_two, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Satisfaction (1-5)", x = "", title = "") +
  scale_x_discrete(labels=c("State_Leg" = "State Leg."))

ds %>%
  drop_na(redist_fair_num_post, redist_type_two) %>%
  group_by(redist_type_two) %>%
  summarise(
    nrow = length(redist_fair_num_post),
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  mutate(redist_type_two = fct_rev(redist_type_two)) %>%
  ggplot(., aes(x = redist_type_two, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Trust (1-4)", x = "") +
  scale_x_discrete(labels=c("State_Leg" = "State Leg."))

### 2.3.2 Referenced statistics ----
# Difference in means
t.test(ds$redist_satisfied_num_post ~ ds$redist_type_two)
t.test(ds$redist_fair_num_post ~ ds$redist_type_two)
## 2.4 Figure 4 ----
### 2.4.1 Plot ----
ds %>% 
  drop_na(redist_satisfied_num_post, redist_type_two, redist_correct) %>%
  group_by(redist_type_two, redist_correct) %>% 
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  mutate(
    redist_type_two = fct_rev(redist_type_two), 
    redist_correct = case_when(
      redist_correct == "no" ~ "Redistricting Knowledge: No", 
      redist_correct == "yes" ~ "Redistricting Knowledge: Yes"
    )
  ) %>%
  ggplot(., aes(x = redist_type_two, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(
    y = "Redistricting Satisfaction (1-5)",
    x = "Primary State Redistricting Authority"
  ) +
  scale_x_discrete(labels=c("State_Leg" = "State Leg.")) +
  facet_grid(~ redist_correct)

### 2.4.2 Referenced statistics ----
# Difference in means
t.test(
  ds$redist_satisfied_num_post[ds$redist_correct=="yes"] ~ 
    ds$redist_type_two[ds$redist_correct=="yes"]
)
# Estimate (in standard deviations)
(
  mean(
    ds$redist_satisfied_num_post[
      ds$redist_correct=="yes" & ds$redist_type_two=="Commission"
    ], 
    na.rm = TRUE
  ) - mean(
    ds$redist_satisfied_num_post[
      ds$redist_correct=="yes" & ds$redist_type_two=="State_Leg"
    ], 
    na.rm = TRUE
    )
  ) / sd(
    ds$redist_satisfied_num_post[ds$redist_correct=="yes"], na.rm = TRUE)
## 2.5 Figure 5 ----
### 2.5.1 Plot ----
ds %>% 
  drop_na(redist_satisfied_num_post, redist_comission_type, redist_correct) %>%
  group_by(redist_comission_type, redist_correct) %>% 
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  mutate(
    redist_correct = case_when(
      redist_correct == "no" ~ "Redistricting Knowledge: No", 
      redist_correct == "yes" ~ "Redistricting Knowledge: Yes"
    )
  ) %>%
  ggplot(., aes(x = redist_comission_type, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci,
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5)
  ) +
  labs(y = "Redistricting Satisfaction \n(1-5)", x = "") +
  scale_x_discrete(
    labels = c(
      "Ind_Commission" = "Independent \nCommission", 
      "Political_Commission" = "Political \nCommission", 
      "State_Leg" = "State \nLegislature"
    )
  ) +
  facet_grid(~ redist_correct) 

ds %>% 
  drop_na(redist_fair_num_post, redist_comission_type, redist_correct) %>%
  group_by(redist_comission_type, redist_correct) %>% 
  summarise(
    nrow = length(redist_fair_num_post), 
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  mutate(
    redist_correct = case_when(
      redist_correct == "no" ~ "Redistricting Knowledge: No", 
      redist_correct == "yes" ~ "Redistricting Knowledge: Yes"
    )
  ) %>%
  ggplot(., aes(x = redist_comission_type, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5)
  ) +
  labs(
    y = "Redistricting Trust\n(1-4)", 
    x = "Primary State Redistricting Authority"
  ) +
  scale_x_discrete(
    labels = c(
      "Ind_Commission" = "Independent \nCommission",
      "Political_Commission" = "Political \nCommission",
      "State_Leg" = "State \nLegislature"
    )
  ) +
  facet_grid(~ redist_correct) 


## 2.6 Figure 6 ----
### 2.6.1 Plot ----
ds %>%
  drop_na(redist_satisfied_num_post, party_match18) %>%
  group_by(party_match18) %>%
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  mutate(
    party_match18 = factor(
      party_match18, 
      levels = c("Opposition", "Independent", "Copartisan")
    )
  ) %>%
  ggplot(., aes(x = party_match18, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(
    y = "Redistricting Satisfaction (1-5)", 
    x = "2022 Congressional Election Result"
  ) +
  scale_x_discrete(
    labels = c(
      "Copartisan" = "Copartisan \n Won", 
      "Independent" = "Independent \n Respondent", 
      "Opposition" = "Copartisan \n Lost"
    )
  )

### 2.6.2 Referenced statistics ----
# Difference in means
t.test(
  ds$redist_satisfied_num_post[
    ds$party_match18 %in% c("Copartisan", "Opposition")
  ] ~ 
    ds$party_match18[ds$party_match18 %in% c("Copartisan", "Opposition")]
)

# Estimate (in standard deviations)
(
  mean(
    ds$redist_satisfied_num_post[
      ds$party_match18=="Copartisan"
    ], 
    na.rm = TRUE
  ) - mean(
    ds$redist_satisfied_num_post[
      ds$party_match18=="Opposition"
    ], 
    na.rm = TRUE
  )
) / sd(
  ds$redist_satisfied_num_post[
    ds$party_match18 %in% c("Copartisan", "Opposition")
  ], 
  na.rm = TRUE
)

## 2.7 Figure 7 ----
### 2.7.1 Plot ----
mod <- lm_robust(
  redist_satisfied_num_post ~ 
    redist_type_two * redist_correct + party_match18 + 
    ideo + resp_gender + age + resp_race + resp_income_num +
    resp_educ + religiosity + new_dist,
  data = ds %>% mutate(redist_type_two = as.character(redist_type_two)),
  weights = teamweight,
  clusters = state,
  se_type = "stata"
)

plot_slopes(
  mod,
  variables = "redist_type_two",
  condition = "redist_correct"
) +
  scale_y_continuous(limits = c(-1, 0.5))+
  geom_hline(yintercept = 0, linetype = "dashed")+
  labs(
    y = "Marginal Effect of State. Leg. Redistricting
    DV: Redistricting Satisfaction (1-5)",
    x = "Redistricting Knowledge"
  ) +
  scale_x_discrete(labels=c("no" = "No", "yes" = "Yes"))

### 2.7.2 Referenced statistics ----
plot_slopes(
  mod,
  variables = "redist_type_two",
  condition = "redist_correct", 
  draw = FALSE
)[2, c(3:4,11)]

## 2.8 Figure 8 ----
### 2.8.1 Plot ----
ds %>%
  drop_na(redist_satisfied_num_post, redist_party_control_match) %>%
  group_by(redist_party_control_match) %>%
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  ggplot(., aes(x = redist_party_control_match, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci), 
    position = position_dodge(width = .5), 
    size = 0.4, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Satisfaction (1-5)", x = "2022 Map Author") +
  scale_x_discrete(
    labels = c(
      "Copartisan" = "State Leg:\n Copartisan",
      "Opposition" = "State Leg:\n Opposition",
      "Independent" = "State Leg:\n Independent",
      "Commission" = "Commission\n", 
      "Court\n" = "Court\n"
    )
  )
